<?php

namespace App\Http\Controllers\home;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\PaymentController;
use App\Models\Activitys;
use App\Models\Addresses;
use App\Models\AdminAgencys;
use App\Models\Appointments;
use App\Models\Areas;
use App\Models\Articles;
use App\Models\BalanceLogs;
use App\Models\BidLists;
use App\Models\BondReturnLogs;
use App\Models\Buys;
use App\Models\Cases;
use App\Models\CashOuts;
use App\Models\Categorys;
use App\Models\Collections;
use App\Models\CompanyInfos;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Feedbacks;
use App\Models\Footprints;
use App\Models\Goods;
use App\Models\GoodsCategorys;
use App\Models\GoodsEvaluates;
use App\Models\IntegralLogs;
use App\Models\Mails;
use App\Models\OrderGoods;
use App\Models\Orders;
use App\Models\ProfitLogs;
use App\Models\Regions;
use App\Models\Services;
use App\Models\SignUps;
use App\Models\Stores;
use App\Models\UserInfos;
use App\Models\Users;
use App\Models\Wechat;
use App\Models\WordDownloadLogs;
use App\Models\Words;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class MyController extends Controller
{
    /**
     * HR个人中心-首页页面
     * @param url home.hrmy.hrIndex
     */
    public function hrIndex(){
        $title = 'HR个人中心-首页';
        return view('home.hrmy.index',compact('title'));
    }
    /**
     * HR个人中心-基本信息页面
     * @param url home.hrmy.hrInfo
     */
    public function hrInfo(){
        $title = 'HR个人中心-基本信息';
        return view('home.hrmy.info',compact('title'));
    }
    /**
     * HR个人中心-基本信息修改页面
     * @param url home.hrmy.hrInfoEdit
     */
    public function hrInfoEdit(){
        $title = 'HR个人中心-基本信息修改';
        return view('home.hrmy.info_edit',compact('title'));
    }
    /**
     * HR个人中心-安全中心页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafe(){
        $title = 'HR个人中心-安全中心';
        return view('home.hrmy.safe',compact('title'));
    }
    /**
     * HR个人中心-安全中心-密码修改页面
     * @param url home.hrmy.hrSafePass
     */
    public function hrSafePass(){
        $title = 'HR个人中心-密码修改';
        return view('home.hrmy.safe_pass',compact('title'));
    }
    /**
     * HR个人中心-安全中心-手机号修改页面
     * @param url home.hrmy.hrSafePhone
     */
    public function hrSafePhone(){
        $title = 'HR个人中心-手机号修改';
        return view('home.hrmy.safe_phone',compact('title'));
    }
    /**
     * HR个人中心-安全中心-微信绑定页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafeWeixin(){
        $title = 'HR个人中心-微信绑定';
        return view('home.hrmy.safe_weixin',compact('title'));
    }
    /**
     * HR个人中心-安全中心-QQ绑定页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafeQQ(){
        $title = 'HR个人中心-QQ绑定';
        return view('home.hrmy.safe_qq',compact('title'));
    }
    /**
     * HR个人中心-收货地址
     * @param url home.hrmy.hrAddress
     */
    public function hrAddress(){
        $title = 'HR个人中心-收货地址';
        return view('home.hrmy.address',compact('title'));
    }
    /**
     * HR个人中心-收货地址-新增
     * @param url home.hrmy.hrAddressAdd
     */
    public function hrAddressAdd(){
        $title = 'HR个人中心-新增收货地址';
        return view('home.hrmy.address_add',compact('title'));
    }
    /**
     * HR个人中心-提现
     * @param url home.hrmy.hrCashOut
     */
    public function hrCashOut(){
        $title = 'HR个人中心-提现';
        return view('home.hrmy.cash_out',compact('title'));
    }
    /**
     * HR个人中心-资产管理中心
     * @param url home.hrmy.hrAssets
     */
    public function hrAssets(){
        $title = 'HR个人中心-资产管理中心';
        return view('home.hrmy.assets',compact('title'));
    }
    /**
     * HR个人中心-资产管理中心-消费记录
     * @param url home.hrmy.hrAssetsConsume
     */
    public function hrAssetsConsume(){
        $title = 'HR个人中心-消费记录';
        return view('home.hrmy.assets_consume',compact('title'));
    }
    /**
     * HR个人中心-订单管理中心
     * @param url home.hrmy.hrOrder
     */
    public function hrOrder(){
        $title = 'HR个人中心-订单管理中心';
        return view('home.hrmy.order',compact('title'));
    }
    /**
     * HR个人中心-订单详情
     * @param url home.hrmy.hrOrderDetails
     */
    public function hrOrderDetails(){
        $title = 'HR个人中心-订单详情';
        return view('home.hrmy.order_details',compact('title'));
    }
    /**
     * HR个人中心-需求管理中心
     * @param url home.hrmy.hrDemand
     */
    public function hrDemand(){
        $title = 'HR个人中心-需求管理中心';
        return view('home.hrmy.demand',compact('title'));
    }
    /**
     * HR个人中心-收藏-店铺
     * @param url home.hrmy.hrCollectStore
     */
    public function hrCollectStore(){
        $title = 'HR个人中心-店铺收藏';
        return view('home.hrmy.collect_store',compact('title'));
    }
    /**
     * HR个人中心-收藏-商品
     * @param url home.hrmy.hrCollectGoods
     */
    public function hrCollectGoods(){
        $title = 'HR个人中心-商品收藏';
        return view('home.hrmy.collect_goods',compact('title'));
    }
    /**
     * HR个人中心-收藏-文档
     * @param url home.hrmy.hrCollectWord
     */
    public function hrCollectWord(){
        $title = 'HR个人中心-文档收藏';
        return view('home.hrmy.collect_word',compact('title'));
    }
    /**
     * HR个人中心-收藏-新闻
     * @param url home.hrmy.hrCollectNews
     */
    public function hrCollectNews(){
        $title = 'HR个人中心-新闻收藏';
        return view('home.hrmy.collect_news',compact('title'));
    }
    /**
     * HR个人中心-收藏-活动
     * @param url home.hrmy.hrCollectAct
     */
    public function hrCollectAct(){
        $title = 'HR个人中心-活动收藏';
        return view('home.hrmy.collect_act',compact('title'));
    }
    /**
     * HR个人中心-下载记录-文档
     * @param url home.hrmy.hrDownloadWord
     */
    public function hrDownloadWord(){
        $title = 'HR个人中心-文档下载记录';
        return view('home.hrmy.download_word',compact('title'));
    }
    /**
     * HR个人中心-活动管理中心
     * @param url home.hrmy.hrActivity
     */
    public function hrActivity(){
        $title = 'HR个人中心-活动管理中心';
        return view('home.hrmy.activity',compact('title'));
    }
    /**
     * HR个人中心-足迹
     * @param url home.hrmy.hrFootprint
     */
    public function hrFootprint(){
        $title = 'HR个人中心-足迹';
        return view('home.hrmy.footprint',compact('title'));
    }
    /**
     * HR个人中心-消息查看中心
     * @param url home.hrmy.hrMail
     */
    public function hrMail(){
        $title = 'HR个人中心-消息查看中心';
        return view('home.hrmy.mail',compact('title'));
    }
    /**
     * HR个人中心-投诉建议列表
     * @param url home.hrmy.hrFeedBack
     */
    public function hrFeedBack(){
        $title = 'HR个人中心-投诉建议列表';
        return view('home.hrmy.feedback',compact('title'));
    }
    /**
     * HR个人中心-投诉建议-编写建议
     * @param url home.hrmy.hrFeedBackAdd
     */
    public function hrFeedBackAdd(){
        $title = 'HR个人中心-编写建议';
        return view('home.hrmy.feedback_add',compact('title'));
    }
    /**
     * HR个人中心-投诉建议-详情
     * @param url home.hrmy.hrFeedBackDetails
     */
    public function hrFeedBackDetails(){
        $title = 'HR个人中心-建议详情';
        return view('home.hrmy.feedback_details',compact('title'));
    }
    /**
     * HR个人中心-平台客服
     * @param url home.hrmy.hrCustomerService
     */
    public function hrCustomerService(){
        $title = 'HR个人中心-平台客服';
        return view('home.hrmy.customer_service',compact('title'));
    }


    /**
     * 供应商个人中心-审核页面
     * @param url home.suppliermy.SupplierExamine
     */
    public function supplierExamine(){
        $title = '供应商个人中心-审核页面';

        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return view('home.suppliermy.examine',compact('title'));
            }else{
                return redirect(route('home.suppliermy.supplierIndex'));
            }
        }
    }
    /**
     * 供应商个人中心-首页
     * @param url home.suppliermy.SupplierIndex
     */
    public function supplierIndex(){
        $title = '供应商个人中心-首页';
//        $user_id = auth()->guard('web')->id();
//        $userinfo = Users::where('id',$user_id)->value('status');
//        if(empty($user_id) || empty($userinfo)){
//            return redirect(route('home.login.login'));
//        }else{
//            if ($userinfo != 1){
//                return redirect(route('home.suppliermy.supplierExamine'));
//            }else{
//                return view('home.suppliermy.index',compact('title'));
//            }
//        }
        return view('home.suppliermy.index',compact('title'));
    }
    /**
     * 供应商个人中心-基本信息页面
     * @param url home.suppliermy.supplierInfo
     */
    public function supplierInfo(){
        $title = '供应商个人中心-基本信息';
        return view('home.suppliermy.info',compact('title'));
    }
    /**
     * 供应商个人中心-基本信息修改页面
     * @param url home.suppliermy.supplierInfoEdit
     */
    public function supplierInfoEdit(){
        $title = '供应商个人中心-基本信息修改';
        return view('home.suppliermy.info_edit',compact('title'));
    }
    /**
     * 供应商个人中心-安全中心页面
     * @param url home.suppliermy.supplierSafe
     */
    public function supplierSafe(){
        $title = '供应商个人中心-安全中心';
        return view('home.suppliermy.safe',compact('title'));
    }
    /**
     * 供应商个人中心-安全中心-密码修改页面
     * @param url home.suppliermy.supplierSafePass
     */
    public function supplierSafePass(){
        $title = '供应商个人中心-密码修改';
        return view('home.suppliermy.safe_pass',compact('title'));
    }
    /**
     * 供应商个人中心-安全中心-手机号修改页面
     * @param url home.suppliermy.supplierSafePhone
     */
    public function supplierSafePhone(){
        $title = '供应商个人中心-手机号修改';
        return view('home.suppliermy.safe_phone',compact('title'));
    }
    /**
     * 供应商个人中心-安全中心-微信绑定页面
     * @param url home.suppliermy.supplierSafeWeixin
     */
    public function supplierSafeWeixin(){
        $title = '供应商个人中心-微信绑定';
        return view('home.suppliermy.safe_weixin',compact('title'));
    }
    /**
     * 供应商个人中心-安全中心-QQ绑定页面
     * @param url home.suppliermy.supplierSafeQQ
     */
    public function supplierSafeQQ(){
        $title = '供应商个人中心-QQ绑定';
        return view('home.suppliermy.safe_qq',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-财务信息管理
     * @param url home.suppliermy.supplierAssetsInfo
     */
    public function supplierAssetsInfo(){
        $title = '供应商个人中心-财务信息管理';
        return view('home.suppliermy.assets_info',compact('title'));
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-余额充值页面
     * @param url home.suppliermy.supplierBalanceAdd
     */
    public function supplierBalanceAdd(){
        $title = '供应商个人中心-余额充值';
        return view('home.suppliermy.balance_add',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-余额充值订单页面
     * @param url home.suppliermy.supplierBalanceAddOrder
     */
    public function supplierBalanceAddOrder(){
        $title = '供应商个人中心-余额充值订单';
        return view('home.suppliermy.balance_add_order',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-余额充值完成页面
     * @param url home.suppliermy.supplierBalanceAddFinish
     */
    public function supplierBalanceAddFinish(){
        $title = '供应商个人中心-余额充值完成';
        return view('home.suppliermy.balance_add_finish',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-总收益明细
     * @param url home.suppliermy.supplierAllProfit
     */
    public function supplierAllProfit(){
        $title = '供应商个人中心-总收益明细';
        return view('home.suppliermy.allProfit',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-商品/活动收益明细
     * @param url home.suppliermy.supplierBuyProfit
     */
    public function supplierBuyProfit(){
        $title = '供应商个人中心-商品/活动收益明细';
        return view('home.suppliermy.buyProfit',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-投标收益明细
     * @param url home.suppliermy.supplierBidProfit
     */
    public function supplierBidProfit(){
        $title = '供应商个人中心-投标收益明细';
        return view('home.suppliermy.bidProfit',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-积分消费记录页面
     * @param url home.suppliermy.supplierIntegralLogs
     */
    public function supplierIntegralLogs(){
        $title = '供应商个人中心-积分消费记录';
        return view('home.suppliermy.integral_logs',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-余额消费记录页面
     * @param url home.suppliermy.supplierBalanceLogs
     */
    public function supplierBalanceLogs(){
        $title = '供应商个人中心-余额消费记录';
        return view('home.suppliermy.profit',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-提现
     * @param url home.suppliermy.supplierCashOut
     */
    public function supplierCashOut(){
        $title = '供应商个人中心-提现';
        return view('home.suppliermy.cash_out',compact('title'));
    }
    /**
     * 供应商个人中心-财务中心-申请退还保证金
     * @param url home.suppliermy.supplierRetreatBond
     */
    public function supplierRetreatBond(){
        $title = '供应商个人中心-申请退还保证金';
        return view('home.suppliermy.retreat_bond',compact('title'));
    }
    /**
     * 供应商个人中心-竞标管理中心
     * @param url home.suppliermy.supplierBidding
     */
    public function supplierBidding(){
        $title = '供应商个人中心-竞标管理中心';
        return view('home.suppliermy.bidding',compact('title'));
    }
    /**
     * 供应商个人中心-活动管理中心
     * @param url home.suppliermy.supplierAct
     */
    public function supplierAct(){
        $title = '供应商个人中心-活动管理中心';
        return view('home.suppliermy.activity',compact('title'));
    }
    /**
     * 供应商个人中心-店铺管理中心-基本信息
     * @param url home.suppliermy.supplierStore
     */
    public function supplierStore(){
        $title = '供应商个人中心-店铺基本信息';
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.store',compact('title'));
            }
        }
    }
    /**
     * 供应商个人中心-店铺管理中心-修改基本信息
     * @param url home.suppliermy.supplierStore
     */
    public function supplierStoreEdit(){
        $title = '供应商个人中心-店铺基本信息修改';
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.store_edit',compact('title'));
            }
        }
    }
    /**
     * 供应商个人中心-店铺管理中心-订单管理
     * @param url home.suppliermy.supplierGoods
     */
    public function supplierOrder(){
        $title = '供应商个人中心-订单管理';
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.order',compact('title'));
            }
        }
    }
    /**
     * 供应商个人中心-商品管理中心
     * @param url home.suppliermy.supplierGoods
     */
    public function supplierGoods(){
        $title = '供应商个人中心-商品管理中心';
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.goods',compact('title'));
            }
        }
    }
    /**
     * 供应商个人中心-发布、修改商品页面
     * @param url home.suppliermy.supplierGoodsUpdate
     * @param Request $request
     * @return bool
     */
    public function supplierGoodsUpdate(){
        $title = '供应商个人中心-编辑商品';
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
        return view('home.suppliermy.goods_update',compact('title'));
            }
        }
    }
    /**
     * 供应商个人中心-足迹
     * @param url home.suppliermy.supplierFootprint
     */
    public function supplierFootprint(){
        $title = '供应商个人中心-足迹';
        return view('home.suppliermy.footprint',compact('title'));
    }
    /**
     * 供应商个人中心-消息查看中心
     * @param url home.suppliermy.supplierMail
     */
    public function supplierMail(){
        $title = '供应商个人中心-消息查看中心';
        return view('home.suppliermy.mail',compact('title'));
    }
    /**
     * 供应商个人中心-投诉建议列表
     * @param url home.suppliermy.supplierFeedBack
     */
    public function supplierFeedBack(){
        $title = '供应商个人中心-投诉建议列表';
        return view('home.suppliermy.feedback',compact('title'));
    }
    /**
     * 供应商个人中心-投诉建议-编写建议
     * @param url home.suppliermy.supplierFeedBackAdd
     */
    public function supplierFeedBackAdd(){
        $title = '供应商个人中心-编写建议';
        return view('home.suppliermy.feedback_add',compact('title'));
    }
    /**
     * 供应商个人中心-投诉建议-详情
     * @param url home.suppliermy.supplierFeedBackDetails
     */
    public function supplierFeedBackDetails(){
        $title = '供应商个人中心-建议详情';
        return view('home.suppliermy.feedback_details',compact('title'));
    }
    /**
     * 供应商个人中心-平台客服
     * @param url home.suppliermy.supplierCustomerService
     */
    public function supplierCustomerService(){
        $title = '供应商个人中心-平台客服';
        return view('home.suppliermy.customer_service',compact('title'));
    }


    /**
     * 用户签到
     * @param url home.user.userSignInApi
     * @return is_sign 是否签到：0否 1是
     */
    public function userSignInApi(){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','sign_in_at','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }
            //当天第一次签到送积分
            if (trim(@date('Y-m-d', strtotime($userinfo['sign_in_at']))) != trim(date('Y-m-d'))) {
                $integral = Configs::getConfig('integral');
                if($userinfo['type'] == 1) {
                    $sign_in = $integral['hr_sign_in'];
                }else {
                    $sign_in = $integral['bus_sign_in'];
                }
                Users::where('id',$user_id)->increment('integral',$sign_in);
                //添加日志记录
                $integral_arr = [
                    'user_id' => $user_id,
                    'user_name' => $userinfo['username'],
                    'integral' => '+'.$sign_in,
                    'desc' => '每日首次签到送积分',
                    'type' => 1,
                ];
                IntegralLogs::create($integral_arr);
            }
            Users::where('id',$user_id)->update(['sign_in_at' => date('Y-m-d H:i:s')]);
            //判断当天是否签到
            $users = Users::where('id',$user_id)->first();
            $users['is_sign'] = 0;//默认未签到
            if (trim(@date('Y-m-d', strtotime($users['sign_in_at']))) == trim(date('Y-m-d'))) {
                $users['is_sign'] = 1;//已签到
            }
            return $this->writeJson(0,$users,'签到成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户信息
     * @param url home.user.userInfoApi
     * @return is_mobile 是否绑定手机号：0否 1是
     * @return is_wechat 是否绑定微信：0否 1是
     * @return is_qq 是否绑定QQ：0否 1是
     * @return is_password 是否设置密码：0否 1是
     * @return is_sign 是否签到：0否 1是
     * @return massage 站内信未读数量
     *
     * @return (array)footprint 足迹
     * @return foot_id 浏览内容id
     * @return type 类型：1供应商 2需求 3商品 4活动 5文档
     * @return created_at 浏览时间
     * @return (array)具体浏览内容
     *
     * 如果是HR
     * @return order_wait_pay 待付款
     * @return order_wait_send 待发货
     * @return order_wait_take 待收货
     * @return order_wait_evaluate 待评价
     * @return collect_store 店铺收藏量
     * @return collect_goods 商品收藏
     * @return collect_act 活动收藏
     * @return collect_word 文档收藏
     * @return collect_news 新闻收藏
     *
     * 如果是供应商
     * @return member_fee 会费
     * @return bidding 投标中
     * @return winning_bid 已中标
     * @return act_sign_up 报名中的活动
     * @return act_finish 已举办的活动
     *
     * @return store_status 1审核成功 2审核失败
     * @return store_praise 好评量
     * @return store_collect 收藏量
     * @return store_call 致电量
     *
     * @return goods_num 上架的商品量
     * @return goods_salenum 商品销量
     * @return goods_collect 商品收藏量
     */
    public function userInfoApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $lists = Users::where(['id'=>$user_id])->where('status','<>',-1)->first();
            if (empty($lists)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            if($lists['status'] == 1){
                $lists['is_mobile'] = 1;//是否绑定手机号：0否 1是
                $lists['is_wechat'] = empty($lists['wechat_id']) ? 0 : 1;//是否绑定微信：0否 1是
                $lists['is_qq'] = empty($lists['qq_id']) ? 0 : 1;//是否绑定QQ：0否 1是
                $lists['is_password'] = empty($lists['password']) ? 0 : 1;//是否设置密码：0否 1是
                $lists['footprint'] = Footprints::allLists(['user_id'=>$lists['id']], ['*'], 5);
                $lists['massage'] = Mails::where(['user_id'=>$lists['id'], 'status'=>1])->count();
                if($lists['type'] == 1){
                    //收藏量
                    $collect_store = Collections::getCounts(['user_id'=>$lists['id'],'type'=>4]);
                    $lists['collect_store'] = strlen($collect_store)>=4 ? '999+' : $collect_store;
                    $collect_goods = Collections::getCounts(['user_id'=>$lists['id'],'type'=>3]);
                    $lists['collect_goods'] = strlen($collect_goods)>=4 ? '999+' : $collect_goods;
                    $collect_act = Collections::getCounts(['user_id'=>$lists['id'],'type'=>5]);
                    $lists['collect_act'] = strlen($collect_act)>=4 ? '999+' : $collect_act;
                    $collect_word = Collections::getCounts(['user_id'=>$lists['id'],'type'=>1]);
                    $lists['collect_word'] = strlen($collect_word)>=4 ? '999+' : $collect_word;
                    $collect_news = Collections::getCounts(['user_id'=>$lists['id'],'type'=>2]);
                    $lists['collect_news'] = strlen($collect_news)>=4 ? '999+' : $collect_news;
                    //订单量
                    $order_wait_pay = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>2]);
                    $lists['order_wait_pay'] = strlen($order_wait_pay)>=4 ? '999+' : $order_wait_pay;
                    $order_wait_send = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>3]);
                    $lists['order_wait_send'] = strlen($order_wait_send)>=4 ? '999+' : $order_wait_send;
                    $order_wait_take = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>4]);
                    $lists['order_wait_take'] = strlen($order_wait_take)>=4 ? '999+' : $order_wait_take;
                    $order_wait_evaluate = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>5,'evaluation_state'=>1]);
                    $lists['order_wait_evaluate'] = strlen($order_wait_evaluate)>=4 ? '999+' : $order_wait_evaluate;
                }else {
                    $lists['member_fee'] = isset($lists['member_fee']) ? '未缴纳' : '已缴纳';
                    //竞标
                    $bidding = BidLists::getCounts(['store_id'=>$lists['id'],'bid_status' => 3]);//投标中
                    $lists['bidding'] = strlen($bidding)>=4 ? '999+' : $bidding;
                    $winning_bid = BidLists::getCounts(['store_id'=>$lists['id'],'bid_status'=>2]);//已中标
                    $lists['winning_bid'] = strlen($winning_bid)>=4 ? '999+' : $winning_bid;
                    //活动
                    $now_time = date('Y-m-d H:i:s');
                    $act_sign_up = Activitys::getCounts(['user_id'=>$lists['id'],'enroll_at'=>$now_time]);
                    $lists['act_sign_up'] = strlen($act_sign_up)>=4 ? '999+' : $act_sign_up;
                    $act_finish = Activitys::getCounts(['user_id'=>$lists['id'],'start_at'=>$now_time]);
                    $lists['act_finish'] = strlen($act_finish)>=4 ? '999+' : $act_finish;
                    //店铺
                    $store_info = Stores::details(['user_id'=>$lists['id']], ['id','status','call','credit','praise','collections']);
//                    if (!empty($store_info)){
//                        $lists['store_id'] = $store_info['id'];
//                        $lists['store_status'] = $store_info['status'];//店铺状态
//                    }
                    $lists['store_id'] = $store_info['id'];
                    $lists['store_status'] = $store_info['status'];//店铺状态
                    $lists['store_call'] = strlen($store_info['call'])>=4 ? '999+' : $store_info['call'];
                    $lists['store_collect'] = strlen($store_info['collections'])>=4 ? '999+' : $store_info['collections'];
                    $lists['praise'] = strlen($store_info['praise'])>=4 ? '999+' : $store_info['praise'];
                    //商品
                    if($store_info['status'] == 1){
                        $goods_num = Goods::where(['store_id'=>$store_info['id'],'status'=>1])->count();
                        $lists['goods_num'] = strlen($goods_num)>=4 ? '999+' : $goods_num;
                        $collect = Goods::where(['store_id'=>$store_info['id'],'status'=>1])->get(['salenum','collect']);
                        foreach ($collect as $k=>$v){
                            $lists['goods_salenum'] += $v['salenum'];
                            $lists['goods_collect'] += $v['collect'];
                        }
                        $lists['goods_salenum'] = strlen($lists['goods_salenum'])>=4 ? '999+' : $lists['goods_salenum'];
                        $lists['goods_collect'] = strlen($lists['goods_collect'])>=4 ? '999+' : $lists['goods_collect'];
                    }
                }


                //19-9-30 by z 增加返回个人部分信息 start
                $infos_one = Users::where('id',$user_id)->first(['nickname','mobile',]);//联系人、联系人电话
                $infos_two = UserInfos::where('user_id',$user_id)//职位、部门、公司名称、公司类型id、公司行业id、公司规模
                ->first(['hr_position','hr_department','company_name','company_type','company_industry','company_size']);
                $company_type = CompanyInfos::where('id',$infos_two['company_type'])->value('name');
                $company_industry = CompanyInfos::where('id',$infos_two['company_industry'])->value('name');
                $company_size = CompanyInfos::where('id',$infos_two['company_size'])->value('name');

                $lists['need_infos'] = array(
                    'nickname' => !empty($infos_one['nickname']) && $infos_one['nickname'] ? $infos_one['nickname'] : '',
                    'mobile' => !empty($infos_one['mobile']) && $infos_one['mobile'] ? $infos_one['mobile'] : '',
                    'hr_position' => !empty($infos_two['hr_position']) && $infos_two['hr_position'] ? $infos_two['hr_position'] : '',
                    'hr_department' => !empty($infos_two['hr_department']) && $infos_two['hr_department'] ? $infos_two['hr_department'] : '',
                    'company_name' => !empty($infos_two['company_name']) && $infos_two['company_name'] ? $infos_two['company_name'] : '',
                    'company_type' => !empty($company_type) && $company_type ? $company_type : '',
                    'company_type_id' => !empty($infos_two['company_type']) && $infos_two['company_type'] ? $infos_two['company_type'] : 0,
                    'company_industry' => !empty($company_industry) && $company_industry ? $company_industry : '',
                    'company_industry_id' => !empty($infos_two['company_industry']) && $infos_two['company_industry'] ? $infos_two['company_industry'] : 0,
                    'company_size' => !empty($company_size) && $company_size ? $company_size : '',
                    'company_size_id' => !empty($infos_two['company_size']) && $infos_two['company_size'] ? $infos_two['company_size'] : 0
                );
                //end


            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户基本信息
     * @route home.user.userBasicInfoApi
     */
    public function userBasicInfoApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $fields = ['id','avatar','username','nickname','sex','mobile','email','type','qq','wechat','area','bank_name','bank_sn'];
            $userinfo = Users::where('id',$user_id)->where('status','<>',-1)->first($fields);
            $infos = UserInfos::where('user_id',$user_id)->first();
            $userinfo['area_name'] = Areas::where('id',$userinfo['area'])->value('name');
            $userinfo['company_name'] = $infos['company_name'];
            //HR
            $userinfo['hr_department'] = $infos['hr_department'];
            $userinfo['hr_position'] = $infos['hr_position'];
            $userinfo['company_type_id'] = CompanyInfos::where('id',$infos['company_type'])->value('id');
            $userinfo['company_type_name'] = CompanyInfos::where('id',$infos['company_type'])->value('name');
            $userinfo['company_industry_id'] = CompanyInfos::where('id',$infos['company_industry'])->value('id');
            $userinfo['company_industry_name'] = CompanyInfos::where('id',$infos['company_industry'])->value('name');
            $userinfo['company_size_id'] = CompanyInfos::where('id',$infos['company_size'])->value('id');
            $userinfo['company_size_name'] = CompanyInfos::where('id',$infos['company_size'])->value('name');
            //供应商
            $userinfo['master_name'] = $infos['master_name'];
            $userinfo['company_email'] = $infos['company_email'];
            $userinfo['license'] = $infos['license'];
            $userinfo['qualification'] = empty($infos['qualification']) ? [] : $infos['qualification'];
            $userinfo['qrganization_code'] = $infos['qrganization_code'];
            $userinfo['store_id'] = Stores::where('user_id',$user_id)->value('id');
            if ($userinfo['type'] == 2){//返回供应商店铺状态
                $userinfo['store_status'] = Stores::where('user_id',$user_id)->value('status');
            }

            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            return $this->writeJson(0,$userinfo);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户基本信息修改
     * @route home.user.userBasicInfoEditApi
     * 共有的
     * @param sex 性别 1男 2女
     * @param qq QQ
     * @param company_name 公司名称
     * HR
     * @param avatar 头像
     * @param nickname 姓名
     * @param email 邮箱
     * @param company_type 公司类型
     * @param company_industry 公司行业
     * @param company_size 公司规模
     * @param link_department HR部门
     * @param link_position HR职位
     * 供应商
     * @param bank_name 开户行
     * @param bank_sn 银行账户
     * @param wechat 微信
     * @param area 区域(地区信息接口：home.public.area 不传参)
     * @param license 营业执照
     * @param qualification 资格证书
     * @param qrganization_code 营业执照组织机构代码
     * @param company_email 供应商公司邮箱
     * @param master_name 供应商负责人姓名
     */
    public function userBasicInfoEditApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::getUserInfo(['id'=>$user_id]);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $userbascinfo = UserInfos::Where('user_id',$user_id)->first();

            $userinfo->sex = (int)$request->post('sex',1);
            $userinfo->qq = (string)$request->post('qq');
            $userbascinfo->company_name = (string)$request->post('company_name');
            if ($userinfo['type'] == 1){//HR
                $userinfo->avatar = (string)$request->post('avatar');
                $userinfo->username = (string)$request->post('nickname');
                $userinfo->nickname = (string)$request->post('nickname');
                $userinfo->email = (string)$request->post('email');
                $userbascinfo->company_type = (int)$request->post('company_type');
                $userbascinfo->company_size = (int)$request->post('company_size');
                $userbascinfo->company_industry = (int)$request->post('company_industry');
                $userbascinfo->hr_department = (string)$request->post('hr_department');
                $userbascinfo->hr_position = (string)$request->post('hr_position');
            }elseif ($userinfo['type'] == 2){//供应商
                $userinfo->username = (string)$request->post('company_name');
                $userinfo->bank_name = (string)$request->post('bank_name');
                $userinfo->bank_sn = (string)$request->post('bank_sn');
                $userinfo->wechat = (string)$request->post('wechat');
                $userinfo->area = (int)$request->post('area');
                $userbascinfo->company_email = (string)$request->post('company_email');
                $userbascinfo->master_name = (string)$request->post('master_name');
                $userbascinfo->license = (string)$request->post('license');
                $userbascinfo->qualification = $request->post('qualification');
                $userbascinfo->qrganization_code = (string)$request->post('qrganization_code');
            }

            $userinfo->save();
            $userbascinfo->save();
            return $this->writeJson(0,$userinfo);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }

    /**
     * 用户安全中心信息
     * @param url home.user.userSafeApi
     * @param Request $request
     * @return bool
     */
    public function userSafeApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::getUserInfo(['id'=>$user_id]);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $info['is_mobile'] = 1;//是否绑定手机号：0否 1是
            $info['is_wechat'] = empty($userinfo['wechat_id']) ? 0 : 1;//是否绑定微信：0否 1是
            $info['is_qq'] = empty($userinfo['qq_id']) ? 0 : 1;//是否绑定QQ：0否 1是
            $info['is_pass'] = empty($userinfo['password']) ? 0 : 1;//是否设置密码：0否 1是
            if(!empty($userinfo['mobile'])){
                $info['mobile'] = mb_substr($userinfo['mobile'],0,3,"utf-8").'*****'.mb_substr($userinfo['mobile'],-3,4,"utf-8");
            }else {
                $info['mobile'] = null;
            }
            return $this->writeJson(0,$info);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户微信、QQ解除绑定
     * @route home.user.userUnbindApi
     * @param type 1:微信解绑 2:QQ解绑
     */
    public function userUnbindApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $type = (int)$request->post('type');
            if($type == 1){
                Users::where('id',$user_id)->update(['wechat_id'=>'']);
            }elseif ($type == 2){
                Users::where('id',$user_id)->update(['qq_id'=>'']);
            }
            return $this->writeJson(0,null, '解绑成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户修改密码
     * @param url home.user.userChangePwdApi
     * @param mobile 手机号
     * @param password 新密码
     * @param confirmc_pwd 确认密码
     * @param code 短信验证码
     */
    public function userChangePwdApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $mobile = (string)$request->post('mobile');
            $password = (string)$request->post('password');
            $confirmc_pwd = (string)$request->post('confirmc_pwd');
            $code = (string)$request->post('code');
            if(empty($mobile) || empty($password) || empty($confirmc_pwd) || empty($code)) return $this->writeJson(1,null,'参数错误');
            if(!preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $mobile)) return $this->writeJson(1,null,'请填写正确的手机号码');
            if($password !== $confirmc_pwd){
                return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
            }

            $info = Users::where(['id'=>$user_id,'mobile'=>$mobile])->where('status','<>',-1)->first();
            if(!empty($info)){
                //检查验证码是否正确
                $cache = cache()->get('sms-'.$mobile);
                if(empty($cache)) return $this->writeJson(1,null,'短信验证码已过期');//过期
                if($cache['code'] != $code) return $this->writeJson(1,null,'短信验证码输入错误');
                cache()->delete('sms-'.$mobile);//删除验证码

                $info->mobile = $mobile;
                $info->password = bcrypt($password);
                $info->save();
                return $this->writeJson(0,null, '修改成功,请重新登录');
            }else{
                return $this->writeJson(1,null, '该用户不存在');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户修改手机号
     * @param url home.user.userChangeMobileApi
     * @param old_mobile 原手机号
     * @param new_mobile 新手机号
     * @param code 短信验证码
     */
    public function userChangeMobileApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $old_mobile = (string)$request->post('old_mobile');
            $new_mobile = (string)$request->post('new_mobile');
            $code = (string)$request->post('code');
            if(empty($old_mobile) || empty($new_mobile) || empty($code)) return $this->writeJson(1,null,'参数错误');
            $str = '/^((13|14|15|16|17|18|19)[0-9]\d{8})$/';
            if(!preg_match($str, $old_mobile) || !preg_match($str, $new_mobile)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            if($old_mobile == $old_mobile){
                return $this->writeJson(1,null,'新手机号和原手机号相同，无需修改');
            }

            $info = Users::where(['id'=>$user_id,'mobile'=>$old_mobile])->where('status','<>',-1)->first();
            if(!empty($info)){
                //检查验证码是否正确
                $cache = cache()->get('sms-'.$new_mobile);
                if(empty($cache)) return $this->writeJson(1,null,'短信验证码已过期');//过期
                if($cache['code'] != $code) return $this->writeJson(1,null,'短信验证码输入错误');
                cache()->delete('sms-'.$new_mobile);//删除验证码

                $info->mobile = $new_mobile;
                $info->save();
                return $this->writeJson(0,null, '修改成功,请重新登录');
            }else{
                return $this->writeJson(1,null, '该用户不存在');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户资产管理信息
     * @param url home.user.userAssetApi
     * @return username 用户名
     * @return integral 积分
     * @return balance 余额
     * @return bond 保证金
     * @return profit 收益
     * @return contract 合同交易额
     * @return cash 提现
     * @return member_fee 会员费
     * @return member_fee_at 会员费有效期
     */
    public function userAssetApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $lists = Users::where(['id'=>$user_id])->where('status','<>',-1)
                ->first(['id','username','integral','balance','bond','profit','contract','cash','member_fee','member_fee_at','status']);
            if (empty($lists)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户获取积分提示
     * @param url home.user.userGetIntegralApi
     * @return  register 注册
     * @return  login 登录
     * @return  sign_in 签到
     * @return  call 打电话（HR）
     * @return  activity 报名活动（HR）
     * @return  goods 购买商品（HR）
     * @return  demand 发布需求（HR）
     * @return  recharge 充值（供应商）
     * @return  commission 收益（供应商）
     */
    public function userGetIntegralApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $user_info = Users::where('id',$user_id)->where('status','<>',-1)->first(['id','type','integral','sign_in_at','login_at']);
            if (empty($user_info)) return $this->writeJson(1,null,'用户不存在');

            $base = Configs::getConfig('integral');
            $lists = [];
            if ($user_info['type'] == 1){
                $lists['register'] = $base['hr_register'];
                $lists['login'] = $base['hr_login'];
                $lists['sign_in'] = $base['hr_sign_in'];
                $lists['call'] = $base['hr_call'];
                $lists['activity'] = $base['hr_activity'];
                $lists['goods'] = $base['hr_goods'];
                $lists['demand'] = $base['hr_demand'];
            }elseif ($user_info['type'] == 2){
                if($user_info['integral'] <= 1000){
                    $lists['register_grade'] = 1;
                }elseif ($user_info['integral'] > 1000 && $user_info['integral'] <= 2000){
                    $lists['register_grade'] = 2;
                }elseif ($user_info['integral'] > 2000 && $user_info['integral'] <= 3000){
                    $lists['register_grade'] = 3;
                }elseif ($user_info['integral'] > 3000 && $user_info['integral'] <= 4000){
                    $lists['register_grade'] = 4;
                }
                $lists['register'] = $base['bus_register'];
                $lists['login'] = $base['bus_login'];
                $lists['sign_in'] = $base['bus_sign_in'];
                $lists['recharge'] = $base['bus_recharge'];
                $lists['commission'] = $base['bus_commission'];
            }
//            dd($lists);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 积分记录列表
     * @param url home.user.hrAddressApi
     * @return user_name 用户名
     * @return integral 操作金额
     * @return desc 操作描述
     * @return created_at 添加时间
     */
    public function userIntegralLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $lists = IntegralLogs::allLists(['user_id'=>$user_id],['user_name','desc','integral','created_at'],$page,10);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 余额记录列表
     * @param url home.user.userBalanceLogsApi
     * @param Request $request
     * @return user_name 用户名
     * @return balance 操作金额
     * @return desc 操作描述
     * @return created_at 添加时间
     */
    public function userBalanceLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $lists = BalanceLogs::allLists(['user_id'=>$user_id],['user_name','desc','balance','created_at'],$page,10);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户提现
     * @param url home.user.userCashOutApi
     * @param amount 提现金额
     * @param bank_no 银行账户
     * @param bank_user 开户行姓名
     * @param phone 提现人手机号
     * @param bank_name 开户行
     * @return bool
     */
    public function userCashOutApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first();
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $amount = $request->post("amount");
            $bank_no = (string)$request->post("bank_no");
            $bank_name = (string)$request->post("bank_name");
            $phone = (string)$request->post("phone");
            $bank_user = (string)$request->post("bank_user");
            if (empty($phone) || empty($amount) || empty($bank_name) || empty($bank_no) || empty($bank_user)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $phone)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            //验证金额是否足够
            if ($amount < 100) {
                return $this->writeJson(1,null,'提现金额不能低于100');
            }
            if($userinfo['type'] == 1){
                if (floatval($userinfo['balance']) < $amount) {
                    return $this->writeJson(1,null,'余额不足');
                }
            }else {
                if (floatval($userinfo['profit']) < $amount) {
                    return $this->writeJson(1,null,'收益不足');
                }
            }

            $data = [];
            $data['pay_sn'] = Buys::makePaySn($user_id);
            $data['user_id'] = $user_id;
            $data['user_name'] = $userinfo['username'];
            $data['phone'] = $phone;
            $data['amount'] = $amount;
            $data['bank_name'] = $bank_name;
            $data['bank_no'] = $bank_no;
            $data['bank_user'] = $bank_user;
            $insert = CashOuts::create($data);
            if (!$insert) {
                return $this->writeJson(1,null,'提现失败');
            }

            if($userinfo['type'] == 1){
                Users::where('id',$user_id)->decrement('balance',$amount);
            }else {
                Users::where('id',$user_id)->decrement('profit',$amount);
                Users::where('id',$user_id)->increment('cash',$amount);
            }
            //生成未处理数据给后台
            $maps = [
                'title' => '未处理提现：'.$userinfo['username'],
                'type' => 8,
            ];
            AdminAgencys::create($maps);
            return $this->writeJson(0,null,'提现成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户足迹
     * @param url home.user.userFootprintApi
     * @param page 页码
     * @return foot_id 浏览内容id
     * @return type 类型：1供应商 2需求 3商品 4活动 5文档
     * @return created_at 浏览时间
     * @return (array)具体浏览内容
     */
    public function userFootprintApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $footprint = Footprints::allLists(['user_id'=>$user_id], ['*'], 10, $page);

            return $this->writeJson(0,$footprint);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户足迹单个/多个删除
     * @param url home.user.userFootprintDelApi
     * @param id 足迹id
     */
    public function userFootprintDelApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $id = $request->post('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $ids = explode(',',$id);
            Footprints::destroy($ids);
            return $this->writeJson(0,null,'删除成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息列表
     * @param url home.user.userMailsListApi
     * @param page 页码
     * @param status 类型 1:未读 2:已读 不传或其他：全部
     * @return title 标题
     * @return content 内容
     * @return status 状态1未读2已读
     * @return created_at 创建时间
     */
    public function userMailsListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            $status = (int)$request->get('status');//类型 1:未读 2:已读
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $params = [];
            $params['user_id'] = $user_id;
            $params['status'] = $status;
            $list = Mails::allLists($params,['id','title','content','status','created_at'],$page,10);

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息详情
     * @param url home.user.userMailsDetailsApi
     * @param maile_id 消息id
     * @return id 消息id
     * @return user_id 用户id
     * @return title 标题
     * @return content 内容
     * @return status 状态1未读2已读
     * @return created_at 创建时间
     */
    public function userMailsDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $maile_id = (int)$request->get('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');

            $lists = Mails::details(['id'=>$maile_id,'user_id'=>$user_id], $fields=['*']);
            Mails::where('id',$maile_id)->update(['status'=>2]);//变为已读

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息标记为已读
     * @param url home.user.userMailsReadApi
     * @param maile_id 消息id(字符串：逗号隔开)
     * @return bool
     */
    public function userMailsReadApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $maile_id = $request->post('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$maile_id);

            Mails::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>2]);//变为已读

            return $this->writeJson(0,null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息删除
     * @param url home.user.userMailsDeleteApi
     * @param maile_id 消息id(字符串：逗号隔开)
     */
    public function userMailsDeleteApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $maile_id = $request->post('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$maile_id);

            Mails::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>-1]);//软删除

            return $this->writeJson(0,null, '删除成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议列表
     * @param url home.user.userFeedbacksListApi
     * @param page 页码
     * @param isreply 是否回复 1:否 2:是 不传或其他：全部
     * @return id id
     * @return user_id 投诉人ID
     * @return content 投诉内容
     * @return created_at 添加时间
     * @return isreply 是否回复 1:否 2:是
     */
    public function userFeedbacksListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            $isreply = (int)$request->get('isreply');//是否回复 1:否 2:是
            if (empty($page)) return $this->writeJson(1,null,'参数错误');

            $lists = Feedbacks::allLists(['user_id'=>$user_id,'isreply'=>$isreply],['id','user_id','content','images','created_at','isreply'],$page,10);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议详情
     * @param url home.user.userFeedbacksDetailsApi
     * @param feed_id 建议id
     * @return content 用户发送的建议内容
     * @return images 用户发送的图片
     * @return created_at 用户发送的时间
     * @return isreply 是否回复 1:否 2:是
     * @return reply_content 平台回复的建议内容
     * @return reply_time 平台回复的时间
     */
    public function userFeedbacksDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $feed_id = (int)$request->get('feed_id');//建议id
            if (empty($feed_id)) return $this->writeJson(1,null,'参数错误');

            $lists = Feedbacks::details(['id'=>$feed_id,'user_id'=>$user_id], $fields=['*']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议添加
     * @param url home.user.userFeedbacksAddApi
     * @param content 内容
     * @param images 图片
     * @return bool
     */
    public function userFeedbacksAddApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $images = (string)$request->post('images');
            if (!empty($images)){
                $images = explode(',',$images);
            }
            $params = [
                'user_id' => $user_id,
                'user_name' => $userinfo['username'],
                'content' => (string)$request->post('content'),
                'images' => $images,
                'created_at' => date('Y-m-d H:i:s'),
            ];
            Feedbacks::create($params);
            return $this->writeJson(0,null, '添加成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议删除
     * @param url home.user.userFeedbacksDelete
     * @param feed_id 建议id(字符串：逗号隔开)
     * @return bool
     */
    public function userFeedbacksDeleteApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $feed_id = $request->post('feed_id');//建议id
            if (empty($feed_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$feed_id);

            Feedbacks::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>-1]);//删除
            return $this->writeJson(0,null, '删除成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址列表
     * @param url home.user.hrAddressApi
     * @param Request $request
     * @return bool
     */
    public function hrAddressApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $address = Addresses::allLists(['user_id'=>$user_id]);

            return $this->writeJson(0,$address);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址详情
     * @param url home.user.hrAddressDetailsApi
     * @param address_id 收货地址id
     * @return realname 姓名
     * @return phone 电话
     * @return province 省份
     * @return city 城市
     * @return region 区域
     * @return detail 详细地址
     */
    public function hrAddressDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $address_id = (int)$request->get("address_id");//收货地址id
            if (empty($address_id)) return $this->writeJson(1,null,'参数错误');

            $address = Addresses::details(['id'=>$address_id,'user_id'=>$user_id]);

            return $this->writeJson(0,$address);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址新增或编辑
     * @param url home.user.hrAddressEditApi
     * @param realname 收货人
     * @param phone 电话
     * @param province 省份id
     * @param city 城市id
     * @param region 区域id
     * @param detail 详细地址
     * @return bool
     */
    public function hrAddressEditApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
                if ($userinfo['type'] != 1) return $this->writeJson(1,null,'供应商没有收获地址!');
            }
            $realname = (string)$request->post("realname");
            $phone = (string)$request->post("phone");
            $province = (int)$request->post("province");
            $city = (int)$request->post("city");
            $region = (int)$request->post("region");
            $detail = (string)$request->post("detail");
            if (empty($user_id) || empty($realname) || empty($phone) || empty($detail)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!empty($phone) && !preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $phone)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            $province_name = Regions::where('id',$province)->value('name');
            $city_name = Regions::where('id',$city)->value('name');
            $region_name = Regions::where('id',$region)->value('name');

            $address = Addresses::where(['user_id' => $user_id])->where('status','<>',-1)->first();
            if(!empty($address)){
                $address->user_id = $user_id;
                $address->realname = $realname;
                $address->phone = $phone;
                $address->province = $province;
                $address->city = $city;
                $address->region = $region;
                $address->area = $province_name . ' ' . $city_name . ' ' .$region_name;
                $address->detail = $detail;
                $address->default = 1;
            }else{
                $address = new Addresses();
                $address->user_id = $user_id;
                $address->realname = $realname;
                $address->phone = $phone;
                $address->province = $province;
                $address->city = $city;
                $address->region = $region;
                $address->area = $province_name . ' ' . $city_name . ' ' .$region_name;
                $address->detail = $detail;
                $address->default = 1;
            }
            $address->save();

            return $this->writeJson(0,null,'操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除收货地址
     * @param url home.user.hrAddressDeleteApi
     * @param address_id 收货地址id
     * @return bool
     */
    public function hrAddressDeleteApi(Request $request) {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $address_id = (int)$request->post("address_id");
            if (empty($address_id)) return $this->writeJson(1,null, '参数错误');
            Addresses::where(['id'=>$address_id, 'user_id'=>$user_id])->update(['status'=>-1]);
            return $this->writeJson(0,null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }

    /**
     * HR订单列表
     * @param url home.user.hrOrderListApi
     * @param page 页码
     * @param state_type 类型：1待付款 2待发货 3待收货 4待评价 不传或其他值：全部
     * @return id 订单id
     * @return order_sn 订单号
     * @return pay_sn 支付单号
     * @return store_id 店铺id
     * @return store_name 店铺名称
     * @return created_at 下单时间
     * @return order_amount 订单金额
     * @return pay_price 支付金额
     * @return order_state 订单状态：1:已取消 2:未付款 3:已付款 4:已发货 5:已收货
     * @return evaluation_state 评价状态 1：未评价 2：已评价
     * @return shipping_code 物流公司
     * @return shipping_sn 物流单号
     * @return pay_method 支付方式 1微信 2支付宝
     * @return is_cancel 是否显示取消订单：0否 1是
     * @return is_pay 是否显示付款：0否 1是
     * @return is_logistics 是否显示查看物流：0否 1是
     * @return is_take 是否显示收货：0否 1是
     * @return is_evaluation 是否显示评价：0否 1是
     * @return is_delete 是否显示删除：0否 1是
     * @return (array)goods_list 订单商品列表
     */
    public function hrOrderListApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get("page",1);
            $state_type = (int)$request->get("state_type");
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $params = [];
            $params['user_id'] = $user_id;
            $params['status'] = -1;
            if ($state_type == 1){
                $params['order_state'] = 2;
            }elseif ($state_type == 2){
                $params['order_state'] = 3;
            }elseif ($state_type == 3){
                $params['order_state'] = 4;
            }elseif ($state_type == 4){
                $params['order_state'] = 5;
                $params['evaluation_state'] = 1;
                $params['refund_state'] = 0;
            }
            $fields = ['id','order_sn','pay_sn','store_id','store_name','created_at','order_amount','pay_price','order_state','evaluation_state','shipping_code','shipping_sn','pay_method'];
            $order_list = Orders::allLists($params,$fields,$page,10,'id','desc',['order_goods']);

            foreach ($order_list as $key => $order) {
                //默认该订单的不可操作性
                $order['is_cancel'] = 0;//取消订单
                $order['is_pay'] = 0;//付款
                $order['is_logistics'] = 0;//查看物流
                $order['is_take'] = 0;//收货
                $order['is_evaluation'] = 0;//评价
                $order['is_delete'] = 0;//删除
                //显示取消订单
                if($order['order_state'] == 2){
                    $order['is_cancel'] = 1;
                    $order['is_pay'] = 1;
                }
                if($order['order_state'] == 3){
                    $order['is_cancel'] = 1;
                }
                //显示收货
                if($order['order_state'] == 4){
                    $order['is_logistics'] = 1;
                    $order['is_take'] = 1;
                }
                //显示评价
                if($order['order_state'] == 5 && $order['evaluation_state'] == 1){
                    $order['is_evaluation'] = 1;
                }
                //显示删除订单
                if($order['order_state'] == 1 || $order['order_state'] == 5){
                    $order['is_delete'] = 1;
                }
                //取商品列表
                $order_list[$key]['goods_list'] =OrderGoods::where('order_id',$order['id'])->get()->toArray();
            }

            return $this->writeJson(0,$order_list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR订单状态操作
     * @param url home.user.hrOrderChangeStateApi
     * @param order_id 订单号
     * @param state_type 类型：1:取消订单  2:确认收货  3:删除订单
     */
    public function hrOrderChangeStateApi(Request $request) {
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $order_id = (int)$request->post("order_id");
            $state_type = (int)$request->post("state_type");
            if (empty($order_id) || empty($state_type)) return $this->writeJson(1,null,'参数错误');

            $params = [];
            $params['id'] = $order_id;
            $params['user_id'] = $user_id;
            $order_info = Orders::getOrderInfo($params,['*']);
            if (empty($order_info)) return $this->writeJson(1,null,'该订单不存在');

            if ($state_type == 1) {//取消订单
                if($order_info['order_state'] == 2 || $order_info['order_state'] == 3){
                    Orders::changeOrderStateCancel($order_info);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else if ($state_type == 2) {//确认收货
                if($order_info['order_state'] == 4){
                    //更新订单状态
                    $update_order = ['order_status' => 2,'order_state' => 5, 'order_time'=>date('Y-m-d H:i:s'), 'finnshed_time'=>date('Y-m-d H:i:s')];
                    Orders::where('id',$order_id)->update($update_order);
                    //HR确认订单，供应商获得收益
                    $member_info = Stores::where('id',$order_info['store_id'])->first(['user_id','seller_name']);
                    Users::where('id',$member_info['user_id'])->increment('profit',$order_info['pay_price']);
                    //收益日志记录
                    $users = Users::where('id',$member_info['user_id'])->first(['id','username']);
                    $order_goods = OrderGoods::where('order_id',$order_id)->pluck('goods_name')->toArray();
                    if (!empty($order_goods)){
                        $tit = '商品名称:'.implode('、',$order_goods);
                    }else{
                        $tit = '购买商品';
                    }
                    $profit = [
                        'user_id' => $users['id'],
                        'user_name' => $users['username'],
                        'profit' => '+'.$order_info['pay_price'],
                        'desc' => $tit,
                        'type' => 1,
                    ];
                    ProfitLogs::create($profit);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else if ($state_type == 3) {
                if($order_info['order_state'] == 1 || $order_info['order_state'] == 5){
                    //删除订单
                    OrderGoods::where('order_id',$order_id)->update(['status'=>-1]);
                    Orders::where('id',$order_id)->update(['status'=>-1]);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else {
                return $this->writeJson(1,null,'参数错误');
            }
            return $this->writeJson(0,null,'操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR评价商品
     * @route home.user.hrGoodsEvaluatesApi
     * @param evaluate_array 评价数组 [order_id 订单id、 goods_id 商品id、content 评价内容、images评价图片(最多5张，逗号隔开)、average_star评价星星]
     */
    public function hrGoodsEvaluatesApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $user_info = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['id','username','avatar','type','status']);
            if (empty($user_info)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($user_info['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($user_info['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($user_info['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
                if($user_info['type'] != 1) return $this->writeJson(1,null, 'HR才能评价商品');
            }

            $evaluate_array = json_decode($request->post('evaluate_array'));//评价数组
            if (empty($evaluate_array)) return $this->writeJson(1,null, '参数不正确');
            foreach ($evaluate_array as $key => $val){
                $val = (array)$this->objectToArray($val);
                //获取订单信息
                $order_info = Orders::getOrderInfo(['id'=>$val['order_id'],'user_id'=>$user_id]);
                //判断订单身份
                if ($order_info['user_id'] != $user_id) return $this->writeJson(1,null, '参数错误');
                //订单为'已收货'状态，并且未评论
                if($order_info['order_state'] != 5 && $order_info['evaluation_state'] != 1){
                    return $this->writeJson(1,null, '订单信息错误');
                }
                //获取当前商品的评价图片
                if (!empty($val['images'])){
                    $evaluate_image = explode(',',$val['images']);
                }else{
                    $evaluate_image = [];
                }
                //默认评语
                $evaluate_comment = $val['content'];
                if (empty($evaluate_comment)) {
                    $evaluate_comment = '不错哦';
                }
                //评分星星
                $average_star = $val['average_star'];
                //评分等级
                if($average_star == 1 || $average_star == 2){
                    $average_type = 3;//差评
                }elseif ($average_star == 3 || $average_star == 4){
                    $average_type = 2;//中评
                }elseif ($average_star == 5){
                    $average_type = 1;//好评
                }
                $insert = [
                    'user_id' => $user_id,
                    'user_name' => $user_info['username'],
                    'user_avatar' => $user_info['avatar'],
                    'goods_id' => $val['goods_id'],
                    'goods_name' => Goods::where('id',$val['goods_id'])->value('title'),
                    'content' => $evaluate_comment,
                    'images' => $evaluate_image,
                    'average_star' => $average_star,
                    'type' => $average_type,
                ];
                GoodsEvaluates::create($insert);
                Orders::where(['id'=>$val['order_id'],'user_id'=>$user_id])->update(['evaluation_state'=>2]);
                Goods::where('id',$insert['goods_id'])->increment('praise');//评论量加1
                //计算好评率
                $all = GoodsEvaluates::where(['goods_id'=>$insert['goods_id']])->where('status','<>',-1)->count();
                $goods = GoodsEvaluates::where(['goods_id'=>$insert['goods_id'], 'type'=>1])->where('status','<>',-1)->count();
                $credit = $goods/$all*100;
                //被评价商品星级
                if($credit <= 20){
                    $star = 1;
                }elseif ($credit >20 && $credit <= 40){
                    $star = 2;
                }elseif ($credit >40 && $credit <= 60){
                    $star = 3;
                }elseif ($credit >60 && $credit <= 80){
                    $star = 4;
                }elseif ($credit >80 && $credit <= 100){
                    $star = 5;
                }
                Goods::where('id',$insert['goods_id'])->update(['credit'=>$credit, 'star'=>$star]);
            }
            return $this->writeJson(0,null, '评价成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null, $e->getMessage());
        }
    }

    /**
     * HR需求管理中心列表
     * @param url home.user.hrDemandListApi
     * @param page 页码
     * @param state 状态 1:审核中 2:已通过 3:已完成 4:进行中 不传或其他：全部
     * @return id 需求id
     * @return user_id 发布需求人id
     * @return title 需求名称
     * @return price 需求预算
     * @return number 竞标人数
     * @return bid_state 状态
     * @return (array)bid_list 竞标供应商
     * @return id 投标id
     * @return store_id 供应商id
     * @return store_name 供应商名称
     * @return bid_status 中标状态 1未中标 2已中标 3投标中
     */
    public function hrDemandListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'页码不能为空');
            $state = (int)$request->get('state');//状态
            if($state == 1){//审核中
                $params['status'] = 2;
            }elseif ($state == 2){
                $params['status'] = 1;
            }elseif ($state == 3){
                $params['status'] = 1;
                $params['bid'] = 3;
            }elseif ($state == 4){
                $params['status'] = 1;
                $params['bid'] = 2;
            }
            $params['user_id'] = $user_id;
            $fields = ['id','user_id','title','price','status','bid','number','limit_num','end_at','created_at'];
            $lists = Demands::centerList($params, $fields, $page, 10);
            foreach ($lists as $key => $val){
                if ($val['number'] == $val['limit_num'] || trim(@date('Y-m-d', strtotime($val['end_at']))) < date('Y-m-d')){
                    $end_state = 1;//已过截止日期或者投标人数已满，不能再投标
                }else{
                    $end_state = 0;//可以投标
                }
                //需求状态判断
                if($val['status'] == 1){
                    $lists[$key]['state'] = 2;//已通过
                    if ($val['bid'] == 2 && $end_state == 0){
                        $lists[$key]['state'] = 4;//投标中
                    }elseif ($val['bid'] == 3 || $val['bid'] == 4 || $end_state == 1){
                        $lists[$key]['state'] = 3;//已中标或已完成
                    }elseif ($val['bid'] == 1){
                        $lists[$key]['state'] = 3;//审核中
                    }
                }elseif ($val['status'] == 2){
                    $lists[$key]['state'] = 1;//审核中
                }elseif ($val['status'] == 3){
                    $lists[$key]['state'] = 5;//审核失败
                }elseif ($val['status'] == 4){
                    $lists[$key]['state'] = 6;//已锁定
                }
                $lists[$key]['start_at'] = trim(@date('Y-m-d', strtotime($val['created_at'])));
                $lists[$key]['price'] = floatval($val['price']);
                $lists[$key]['bid_list'] = BidLists::allLists(['user_id'=>$user_id,'demand_id'=>$val['id']], ['id','store_id','store_name','bid_status']);
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR收藏列表
     * @param url home.user.hrCollectListApi
     * @param page 页码
     * @param type 类型 1文档，2新闻，3商品，4店铺，5活动 (必传)
     * @return id 收藏id
     * @return collect_id 收藏内容id
     * @return type 收藏类型 1文档，2新闻，3商品，4店铺，5活动
     */
    public function hrCollectListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $type = (int)$request->get('type');//类型 1文档，2新闻，3商品，4店铺，5活动
            $page = (int)$request->get('page',1);//页码
            if (empty($type) || empty($page)) return $this->writeJson(1,null,'参数错误');

            $lists = Collections::allLists(['user_id'=>$user_id, 'type'=>$type], ['id','collect_id','type'],$page,10);

            foreach ($lists as $key=>$val) {
                if ($val['type'] == 1){
                    $fields = ['id','thumb','title','summary','view','download','format'];
                    $word = Words::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['word_id'] = $word['id'];
                    $lists[$key]['word_thumb'] = $word['thumb'];
                    $lists[$key]['word_title'] = $word['title'];
                    $lists[$key]['word_summary'] = $word['summary'];
                    $lists[$key]['word_view'] = strlen($word['view'])>=4 ? '999+' : $word['view'];
                    $lists[$key]['word_download'] = strlen($word['download'])>=4 ? '999+' : $word['download'];
                    $lists[$key]['word_format'] = $word['format'];
                }elseif ($val['type'] == 2){
                    $fields = ['id','thumb','title','content','view','collect','created_at'];
                    $news = Articles::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['news_id'] = $news['id'];
                    $lists[$key]['news_thumb'] = $news['thumb'];
                    $lists[$key]['news_title'] = $news['title'];
                    $lists[$key]['news_content'] = str_limit(strip_tags($news['content']) , 170 , '...' );//str_limit 字符截取 strip_tags 去除HTML标签
                    $lists[$key]['news_view'] = strlen($news['view'])>=4 ? '999+' : $news['view'];
                    $lists[$key]['news_collect'] = strlen($news['collect'])>=4 ? '999+' : $news['collect'];
                    $lists[$key]['news_created_at'] = $news['created_at'];
                }elseif ($val['type'] == 3){
                    $fields = ['id','title','thumb','price','description','view','salenum','type','cate_id','cate_pid'];
                    $goods = Goods::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['goods_id'] = $goods['id'];
                    $lists[$key]['goods_thumb'] = $goods['thumb'];
                    $lists[$key]['goods_title'] = $goods['title'];
                    $lists[$key]['goods_price'] = $goods['price'];
                    $lists[$key]['goods_description'] = $goods['description'];
                    $lists[$key]['goods_type'] = $goods['type'];
                    $lists[$key]['goods_view'] = strlen($goods['view'])>=4 ? '999+' : $goods['view'];
                    $lists[$key]['goods_salenum'] = strlen($goods['salenum'])>=4 ? '999+' : $goods['salenum'];
                }elseif ($val['type'] == 4){
                    $fields = ['id','store_name','logo','call','view','service','credit','service_scope','hot','quality'];
                    $store = Stores::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['store_id'] = $store['id'];
                    $lists[$key]['store_name'] = $store['store_name'];
                    $lists[$key]['store_logo'] = $store['logo'];
                    $lists[$key]['store_view'] = strlen($store['view'])>=4 ? '999+' : $store['view'];
                    $lists[$key]['store_call'] = strlen($store['call'])>=4 ? '999+' : $store['call'];
                    $lists[$key]['store_service'] = strlen($store['service'])>=4 ? '999+' : $store['service'];
                    $lists[$key]['store_service_scope'] = $store['store_service_scope'];
                    $lists[$key]['store_hot'] = $store['hot'];
                    $lists[$key]['store_quality'] = $store['quality'];
                }elseif ($val['type'] == 5){
                    $now_time = date('Y-m-d H:i:s');
                    $fields = ['id','thumb','title','cate_pid','cate_id','view','cost','number','enroll_at','start_at'];
                    $act = Activitys::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['act_id'] = $act['id'];
                    $lists[$key]['act_thumb'] = $act['thumb'];
                    $lists[$key]['act_title'] = $act['title'];
                    $lists[$key]['act_cost'] = $act['cost'];
                    $lists[$key]['act_view'] = strlen($act['view'])>=4 ? '999+' : $act['view'];
                    $lists[$key]['act_number'] = strlen($act['number'])>=4 ? '999+' : $act['number'];
                    $lists[$key]['act_enroll_at'] = $act['enroll_at'];
                    $lists[$key]['act_start_at'] = $act['start_at'];
                    if($now_time <= $act['enroll_at']){
                        $lists[$key]['act_state'] = '报名中';
                    }elseif ($now_time >= $act['start_at']){
                        $lists[$key]['act_state'] = '已举办';
                    }else {
                        $lists[$key]['act_state'] = '报名结束';
                    }
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR取消收藏
     * @param url home.user.hrCollectCancelApi
     * @param collect_id 收藏id
     * @return type 类型 1文档，2新闻，3商品，4店铺，5活动 (不能不传)
     * @return bool
     */
    public function hrCollectCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $collect_id = (int)$request->post('collect_id');//收藏内容id
            $type = (int)$request->post('type',1);//类型 1文档，2新闻，3商品，4店铺，5活动
            if (empty($type) || empty($collect_id)) return $this->writeJson(1,null,'参数错误');
            Collections::where(['user_id'=>$user_id, 'id'=>$collect_id,'type'=>$type])->delete();

            return $this->writeJson(0,null, '取消收藏成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR文档下载记录列表
     * @param url home.user.hrWordListApi
     * @return id 下载记录id
     * @return word_id 文档id
     * @return user_id 下载人id
     * @return created_at 下载时间
     * @return word_id 文档id
     * @return word_title 文档标题
     * @return word_format 文档格式
     */
    public function hrWordListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $list = WordDownloadLogs::allLists(['user_id'=>$user_id],['*'],$page,10);
            foreach ($list as $key => $val){
                $word = Words::where('id',$val['word_id'])->first(['id','title','format']);
                $list[$key]['word_id'] = $word['id'];
                $list[$key]['word_title'] = $word['title'];
                $list[$key]['word_format'] = $word['format'];
            }

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR删除文档下载记录
     * @param url home.user.hrWordCancelApi
     * @param download_id 下载记录id
     */
    public function hrWordCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $download_id = (int)$request->post('download_id');//下载记录id
            if (empty($download_id)) return $this->writeJson(1,null,'参数错误');
            WordDownloadLogs::where(['user_id'=>$user_id, 'id'=>$download_id])->update(['status'=>-1]);

            return $this->writeJson(0,null, '删除文档下载记录成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR活动管理中心列表
     * @param url home.user.hrActListApi
     * @param page 页码
     * @param type 类型 1:进行中 2:已举办 3:已取消 不传或其他:全部
     * @return id 活动id
     * @return title 活动标题
     * @return sponsor 活动主办方
     * @return start_at 活动开始时间
     * @return act_state 活动状态 1:进行中 2:已举办 3:已取消
     */
    public function hrActListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            $type = (int)$request->get('type');//类型 1:进行中 2:已举办 3:已取消 不传或其他:全部
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $now_time = date('Y-m-d H:i:s');
            $params = [];
            if($type == 1){
                $params['status'] = 1;
                $params['sign_at'] = $now_time;
            }elseif ($type == 2){
                $params['status'] = 1;
                $params['start_at'] = $now_time;
            }elseif ($type == 3){
                $params['status'] = 4;
            }

            $activity_id = [];
            $info = SignUps::where(['user_id'=>$user_id,'pay_state'=>2])->where('status','<>',-1)->get(['id','activity_id','user_id'])->toArray();
            if (empty($info)){
                $list = new LengthAwarePaginator(
                    array_slice([], ($page * 10) - 10, 10, false),
                    count([]),
                    10, $page,
                    ['path' => $request->url(), 'query' => $request->query()]
                );
            }else {
                foreach ($info as $key => $val){
                    array_push($activity_id, $val['activity_id']);
                }
                $params['id'] = $activity_id;
                $list = Activitys::allLists($params,['id','title','sponsor','start_at','cate_pid','cate_id','status'],$page,10,'start_at');
                foreach ($list as $k=>$v){
                    if($v['status'] == 1){
                        if($now_time < $v['start_at']){
                            $list[$k]['act_state'] = 1;//进行中
                        }else {
                            $list[$k]['act_state'] = 2;//已举办
                        }
                    }elseif ($v['status'] == 4) {
                        $list[$k]['act_state'] = 3;//已取消
                    }
                }
            }

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }


    /**
     * 供应商审核结果
     * @param url home.user.hrAddressApi
     * @param Request $request
     * @return status 1:审核中 0:审核失败
     */
    public function supplierExamineApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    $lists['status'] = 1;
                }elseif($userinfo['status'] == 3){
                    $lists['status'] = 0;
                }elseif ($userinfo['status'] == 4){
                    $lists['status'] = 0;
                }elseif ($userinfo == 1){
                    return redirect(route('home.suppliermy.index'));
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商收益记录列表
     * @route home.user.supplierProfitLogsApi
     *
     * @param page 页码
     * @param type 类型 1:商品/活动收益 2:投标收益 其他或不传：总收益
     *
     * @return desc 名称
     * @return created_at 时间
     * @return profit 金额
     * @return type 类型 1:商品/活动收益 2:投标收益 其他或不传：总收益
     */
    public function supplierProfitLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');

            $params = [
                'user_id'=>$user_id,
                'type'=>(int)$request->get('type'), //1:商品/活动收益 2:投标收益 其他或不传：总收益
            ];
            $lists = ProfitLogs::allLists($params, ['*'], $page, 10);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商申请退货保证金
     * @param url home.user.supplierBondReturnApi
     * @param bond 退还金额
     * @param desc 原因
     * @param link_name 联系人
     * @param link_phone 联系电话
     */
    public function supplierBondReturnApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','bond','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $data = [];
            $data['user_id'] = $user_id;
            $data['user_name'] = $userinfo['username'];
            $data['bond'] = (int)$request->post('bond');
            $data['desc'] = (string)$request->post('desc');
            $data['link_name'] = (string)$request->post('link_name');
            $data['link_phone'] = (string)$request->post('link_phone');
            if (empty($data['bond']) ||empty($data['desc']) ||empty($data['link_name']) ||empty($data['link_phone'])) return $this->writeJson(1,null,'参数错误');

            if($data['bond'] !== (int)$userinfo['bond']){
                return $this->writeJson(1,null,'退还金额错误');
            }
            BondReturnLogs::create($data);
            //生成未处理数据给后台
            $maps = [
                'title' => '未处理退还保证金：'.$userinfo['username'],
                'type' => 9,
            ];
            AdminAgencys::create($maps);
            return $this->writeJson(0,null,'申请成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商竞标管理中心列表
     * @param url home.user.supplierDemandListApi
     * @param page 页码
     * @param bid_status 状态 1:未中标 2:已中标 3:已投标
     * @return demand_id 需求id
     * @return demand_title 需求标题
     * @return demand_price 需求预算
     * @return bid_status 状态 1:未中标 2:已中标 3:已投标
     */
    public function supplierDemandListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'页码不能为空');
            $params = [
                'store_id' => $user_id,
                'bid_status' => (int)$request->get('bid_status'),
            ];
            $lists = BidLists::allLists($params, ['id','demand_id','bid_status','created_at'],10 ,$page);
            foreach ($lists as $key => $val){
                $demand = Demands::where('id',$val['demand_id'])->where('status','<>',-1)->first(['id','title','price']);
                $lists[$key]['demand_title'] = $demand['title'];
                $lists[$key]['demand_price'] = floatval($demand['price']);
                $lists[$key]['start_at'] = trim(@date('Y-m-d', strtotime($val['created_at'])));
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商活动管理中心列表
     * @route home.user.supplierActListApi
     * @param page 页码
     * @param type 类型 1:审核中 2:进行中 3:已结束 4:审核失败 5:已取消 不传或其他:全部
     * @return act_state 1:审核中 2:进行中 3:已结束 4:审核失败 5:已取消
     */
    public function supplierActListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get('page',1);//页码
            $type = (int)$request->get('type');//类型 1:审核中 2:进行中 3:已结束 4:审核失败 5:已取消 不传或其他:全部
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $now_time = date('Y-m-d H:i:s');
            $params = [];
            $params['user_id'] = $user_id;
            if($type == 1){
                $params['status'] = 2;
            }elseif ($type == 2){
                $params['status'] = 1;
                $params['sign_at'] = $now_time;
            }elseif ($type == 3){
                $params['status'] = 1;
                $params['start_at'] = $now_time;
            }elseif ($type == 4){
                $params['status'] = 3;
            }elseif ($type == 5){
                $params['status'] = 4;
            }
            $lists = Activitys::allLists($params,['id','title','number','enroll_at','start_at','cate_pid','cate_id','status'],$page,10,'start_at');
            foreach ($lists as $K=>$v){
                if($v['status'] == 2){
                    $lists[$K]['act_state'] = 1;//审核中
                }elseif ($v['status'] == 1){
                    if($v['start_at'] > $now_time){
                        $lists[$K]['act_state'] = 2;//进行中
                    }else{
                        $lists[$K]['act_state'] = 3;//已结束
                    }
                }elseif ($v['status'] == 3){
                    $lists[$K]['act_state'] = 4;//审核失败
                }elseif ($v['status'] == 4){
                    $lists[$K]['act_state'] = 5;//已取消
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商查看活动报名人信息列表
     * @route home.user.supplierActInfoApi
     *
     * @param activity_id 活动id
     * @param page 页码
     *
     * @return user_name 报名人姓名
     * @return number 报名人数
     * @return sex 报名人姓别
     * @return phone 报名人电话
     * @return email 报名人邮箱
     * @return qq 报名人QQ
     * @return company 公司名称
     * @return department 部门
     * @return position 职位
     * @return type 公司类型
     * @return industry 公司行业
     * @return size 规模
     */
    public function supplierActInfoApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $activity_id = (int)$request->get('activity_id');//活动id
            $page = (int)$request->get('page',1);//页码
            if (empty($activity_id) || empty($page)) return $this->writeJson(1,null,'参数错误');

            $fields = ['id','activity_id','activity_title','user_id','user_name','number','sex','phone','email','qq','company','department','position','type','industry','size'];
            $lists = SignUps::allLists(['activity_id'=>$activity_id],$fields,$page,10);
            foreach ($lists as $key => $val){
                $lists[$key]['type'] = CompanyInfos::where('id',$val['type'])->value('name');
                $lists[$key]['industry'] = CompanyInfos::where('id',$val['industry'])->value('name');
                $lists[$key]['size'] = CompanyInfos::where('id',$val['size'])->value('name');
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商取消活动
     * @param url home.user.supplierActCancelApi
     * @param act_id 活动id
     * @return status 1:审核中 0:审核失败
     */
    public function supplierActCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $act_id = (int)$request->post('act_id');
            if (empty($act_id)) return $this->writeJson(1,null,'参数错误');
            Activitys::where(['id'=>$act_id,'user_id'=>$user_id])->update(['status'=>4]);

            $act_name = Activitys::where('id',$act_id)->value('title');
            $sign_ups = SignUps::where('activity_id',$act_id)->get(['user_id','user_name','money']);
            foreach ($sign_ups as $key => $val){
                Users::where('id',$val['user_id'])->increment('balance',$val['money']);
                //发送站内信-活动费用退还余额通知
                $mails = [
                    'title' => '活动费用退还余额',
                    'content' => '您参加购买的活动' . $act_name .'已取消，购买费用'.$val['money'].'已退还您余额',
                    'user_id' => $val['user_id'],
                    'status' => 1,
                    'type' => 1,
                ];
                Mails::create($mails);
                //余额日志记录
                $balance = [
                    'user_id' => $val['user_id'],
                    'user_name' => $val['user_name'],
                    'balance' => '+'.$val['money'],
                    'desc' => '已取消活动费用退还余额',
                    'type' => 1,
                ];
                BalanceLogs::create($balance);
            }

            return $this->writeJson(0,null, '取消成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商店铺基本信息
     * @param url home.user.supplierStoreApi
     * @return logo logo
     * @return banner banner
     * @return store_name 名称
     * @return service_area 服务区域（调：home.public.area）
     * @return area_info 地址
     * @return workingtime 工作时间
     * @return description 简介
     * @return image 店铺图片
     * @return partners 合作商
     * @return cid 擅长领域
     * @return services 服务内容
     * @return cases 成功案例
     */
    public function supplierStoreApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $fields = ['id','logo','store_name','banner','service_area','area_info','cate_pid','cate_id','workingtime','description','image','partners'];
            $lists = Stores::where(['user_id'=>$user_id])->where('status','<>',-1)->first($fields);
            if(!empty($lists)){
                //擅长领域
                if(!empty($lists['cate_id']) && !empty($lists['cate_pid'])){
                    $ids = $lists['cate_id'].",".$lists['cate_pid'];
                }else{
                    if(!empty($lists['cate_id'])){
                        $ids = $lists['cate_id'];
                    }elseif (!empty($lists['cate_pid'])){
                        $ids = $lists['cate_pid'];
                    }
                }
                $ids = !empty($ids) ? explode(',',$ids) : 0;
                if ($ids !== 0){
                    foreach ($ids as $k => $v){
                        $pid=Categorys::getPid($v);
                        if($pid > 0){
                            $pids[$k][]="$pid";
                            $pids[$k][]=$v;
                        }else{
                            $pids[$k][]=$v;
                        }
                    }
                    $lists['cid'] = $pids;
                }else{
                    $lists['cid'] = null;
                }
                if (!empty($lists['cate_id'])){
                    $lists['cate_id'] = explode(',',$lists['cate_id']);
                    foreach ($lists['cate_id'] as $k => $v){
                        $pidTree[] = Categorys::where('id',$v)->first('name');
                    }
                }else{
                    $pidTree['name'] = [];
                }
                $lists['cate_id'] = $pidTree;
                //服务区域
                if (!empty($lists['service_area'])){
                    $lists['service_area'] = explode(',',$lists['service_area']);
                    foreach ($lists['service_area'] as $k => $v){
                        $pidTree1[] = Areas::where('id',$v)->orderBy('sort','asc')->first(['id','name']);
                        $area[]=(int)$v;
                    }
                    $lists['service_area_id'] = $area;
                    $lists['service_area'] = $pidTree1;
                }else{
                    $lists['service_area_id'] = null;
                    $lists['service_area'] = [];
                }
                $lists['workingtime'] = explode('-',$lists['workingtime']);
                $lists['services'] = Services::where(['store_id' => $lists['id']])->where('status','<>',-1)->get(['title','thumb','images','content']);
                $lists['cases'] = Cases::where(['store_id' => $lists['id']])->where('status','<>',-1)->get(['title','thumb','images','content']);

//                if(!empty($lists['banner'])){
//                    foreach ($lists['banner'] as $key => $val){
//                        $banner[$key]['name'] = substr($val,strripos($val,"/")+1);
//                        $banner[$key]['url'] = $val;
//                    }
//                    $lists['banner'] = $banner;
//                }
//                if(!empty($lists['image'])){
//                    foreach ($lists['image'] as $key => $val){
//                        $images[$key]['name'] = substr($val,strripos($val,"/")+1);
//                        $images[$key]['url'] = $val;
//                    }
//                    $lists['image'] = $images;
//                }
//                if(!empty($lists['partners'])){
//                    foreach ($lists['partners'] as $key => $val){
//                        $partners[$key]['name'] = substr($val,strripos($val,"/")+1);
//                        $partners[$key]['url'] = $val;
//                    }
//                    $lists['partners'] = $partners;
//                }
            }else{
                $lists = null;
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商开店、修改店铺信息
     * @route home.user.supplierStoreUpdateApi
     * @param post_type 1:店铺修改页点击预览 2:店铺修改页点击提交审核 3:从预览页点击提交审核
     * @param logo logo
     * @param store_name 公司名称
     * @param area_info 地址
     * @param workingtime 工作时间
     * @param description 简介
     * @param cid 擅长领域(数组)
     * @param service_area 区域id(逗号隔开字符串)
     * @param banner 图片(逗号隔开字符串)
     * @param image 图片(逗号隔开字符串)
     * @param partners 合作伙伴(逗号隔开字符串)
     * @param case 成功案例(二维数组)
     * @param service 服务内容(二维数组)
     */
    public function supplierStoreUpdateApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $data = $request->all();
//            dd($data);
            if ($data['post_type'] == 3){//从预览页点击提交审核、直接修改店铺状态为审核中
                Stores::where('user_id',$user_id)->update(['status'=>2]);
                $store_name = Stores::where('user_id',$user_id)->value('store_name');
                //生成未处理数据给后台
                $maps = [
                    'title' => '未处理店铺：'.$store_name,
                    'type' => 1,
                ];
                AdminAgencys::create($maps);
                return $this->writeJson(0,$data['store_id'],'操作成功');
            }else{//创建数据
                $state = 2;
                if ($data['post_type'] == 1){//预览
                    $state = 5;
                }elseif ($data['post_type'] == 2){//提交审核
                    $state = 2;
                }

                $data['user_id'] = $user_id;
                $data['seller_name'] = $userinfo['username'];
                $data['status'] = $state;
                //成功案例 json转数组
                $case = json_decode($data['case']);
                if (!empty($case)){
                    foreach ($case as $k => $v){
                        $case_arr[] = (array)$this->objectToArray($v);
                    }
                }
                //服务内容 json转数组
                $service = json_decode($data['service']);
                if (!empty($service)){
                    foreach ($service as $k => $v){
                        $service_arr[] = (array)$this->objectToArray($v);
                    }
                }
                unset($data['post_type']);
                unset($data['case']);
                unset($data['service']);
                $store_info = Stores::where('user_id',$user_id)->where('status','<>',-1)->first();
                if(!empty($store_info)){
                    $info = Stores::findOrFail($store_info['id']);
                    $info->update($data);
                }else{
                    $title = Stores::where('store_name',$data['store_name'])->where('status','<>',-1)->value('store_name');
                    if (!empty($title)){
                        return $this->writeJson(1,null,'该店铺名称已存在，请重新输入');
                    }
                    Stores::create($data);
                }

                $store_id = Stores::where('user_id',$user_id)->where('status','<>',-1)->value('id');
                //成功案例-先删除再添加
                $case_ids = Cases::where('store_id',$store_id)->get(['id']);
                if (!empty($case_arr)){
                    foreach ($case_arr as $value){
                        $case_data = [
                            'store_id'  => $store_id,
                            'thumb'     => $value['imageUrl'],
                            'title'     => $value['title'],
                            'images'   => $value['contentImg'],
                            'content'   => $value['content'],
                        ];
                        if(empty($case_ids)){
                            Cases::create($case_data);
                        }else{
                            foreach($case_ids as $v){
                                Cases::destroy($v['id']);
                            }
                            Cases::create($case_data);
                        }
                    }
                }
                //服务内容-先删除再添加
                $service_ids = Services::where('store_id',$store_id)->where('status','<>',-1)->get(['id']);
                if (!empty($service_arr)){
                    foreach ($service_arr as $value){
                        $service_data = [
                            'store_id'  => $store_id,
                            'thumb'     => $value['imageUrl'],
                            'title'     => $value['title'],
                            'images'   => $value['contentImg'],
                            'content'   => $value['content'],
                        ];
                        if(empty($service_ids)){
                            Services::create($service_data);
                        }else{
                            foreach($service_ids as $v){
                                Services::destroy($v['id']);
                            }
                            Services::create($service_data);
                        }
                    }
                }
                //将供应商logo作为头像
                Users::where('id',$user_id)->update(['avatar'=>$data['logo']]);
                if($state == 2){
                    //生成未处理数据给后台
                    $maps = [
                        'title' => '未处理店铺：'.$data['store_name'],
                        'type' => 1,
                    ];
                    AdminAgencys::create($maps);
                }
                return $this->writeJson(0,$store_id,'操作成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商订单管理
     * @param url home.user.supplierOrderListApi
     * @param page 页码
     * @param state_type 类型：1待发货 不传或其他值：全部
     * @return id 订单id
     * @return order_sn 订单号
     * @return pay_sn 支付单号
     * @return store_id 店铺id
     * @return store_name 店铺名称
     * @return created_at 下单时间
     * @return order_amount 订单金额
     * @return pay_price 支付金额
     * @return order_state 订单状态：1:已取消 2:未付款 3:已付款 4:已发货 5:已收货
     * @return evaluation_state 评价状态 1：未评价 2：已评价
     * @return shipping_code 物流公司
     * @return shipping_sn 物流单号
     * @return pay_method 支付方式 1微信 2支付宝
     * @return is_send 是否显示发货：0否 1是
     * @return (array)goods_list 订单商品列表
     */
    public function supplierOrderListApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }

            $page = (int)$request->get("page",1);
            $state_type = (int)$request->get("state_type");
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $params = [];
            $store_id = Stores::where('user_id',$user_id)->where('status','<>',-1)->value('id');
            if (empty($store_id)) return $this->writeJson(1,null,'您还未开店，请前往个人中心-店铺管理开店');
            $params['store_id'] = $store_id;
            if ($state_type == 1){
                $params['pay_status'] = 2;
                $params['shipping_status'] = 1;
                $params['order_state'] = 3;
            }
            $fields = ['id','order_sn','pay_sn','store_id','store_name','created_at','order_amount','pay_price','pay_status','shipping_status','order_state','evaluation_state','shipping_code','shipping_sn','pay_method','reciver_info'];
            $order_list = Orders::allLists($params,$fields,$page,10,'id','desc',['order_goods']);

            foreach ($order_list as $key => $order) {
                $order_list[$key]['reciver_info'] = unserialize($order['reciver_info']);
                //默认该订单的不可操作性
                $order['is_send'] = 0;//发货
                //显示发货
                if($order['pay_status'] == 2 && $order['shipping_status'] == 1 && $order['order_state'] == 3){
                    $order['is_send'] = 1;//发货
                }
                //取商品列表
                $order_list[$key]['goods_list'] = OrderGoods::where('order_id',$order['id'])->get()->toArray();
            }
//            dd($order_list->toArray());
            return $this->writeJson(0,$order_list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商订单管理-发货
     * @param home.user.supplierDelivery
     * @param order_id 订单id
     * @param shipping_sn 物流单号
     * @param shipping_code 物流公司
     */
    public function supplierDelivery(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status','openid']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $order_id = (int)$request->post('order_id');
            $shipping_sn = (string)$request->post('shipping_sn');
            $shipping_code= (string)$request->post('shipping_code');

            if(empty($order_id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(empty($shipping_sn)){
                return $this->writeJson(1,null,'发货单号不能为空！');
            }
            if(empty($shipping_code)){
                return $this->writeJson(1,null,'配送方式不能为空！');
            }

            $info = Orders::findOrFail($order_id);
            if(empty($info)){
                return $this->writeJson(1,null,'订单不存在！');
            }
            if($info['pay_status'] == 1){
                return $this->writeJson(1,null,'请先支付订单！');
            }
            if($info['shipping_status'] == 2 || $info['order_state'] == 4){
                return $this->writeJson(1,null,'订单已发货！');
            }

            $update_arr = [];
            $update_arr['shipping_status'] = 2;
            $update_arr['shipping_sn'] = $shipping_sn;
            $update_arr['shipping_code'] = $shipping_code;
            $update_arr['shipping_time'] = date("Y-m-d H:i:s");
            $update_arr['order_state'] = Orders::ORDER_STATE_4;
            $info->update($update_arr);
            //发货成功、推送微信消息
            $wechat_user = Users::where('id',$info['user_id'])->first(['id','openid']);
            if (!empty($wechat_user['openid'])){
                $template_id = 'W4QL6YSFRclJrMceHB9vJfLnJb88bjOw9JvNCHmnyzs';
                $url = 'http://wap.hrqidian.com/#/myOrder?state_type=0';
                $data = [
                    'first' => '亲，您的商品已发货，请注意个人中心订单管理追踪查收',
                    'keyword1' => $info['order_sn'],
                    'keyword2' => OrderGoods::where('order_id',$order_id)->value('goods_name'),
                    'keyword3' => $info['pay_price'].'元',
                    'keyword4' => $update_arr['shipping_time'],
                    'remark' => '虚拟商品（如软件、系统等）请联系小启客服为你提供下载地址 HR小启客服',
                ];
                Wechat::wechatSend($wechat_user['openid'], $template_id, $url, $data);
            }

            return $this->writeJson(0,null,'发货成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品管理中心列表
     * @param url home.user.supplierGoodsListsApi
     * @param Request $request
     * @return bool
     * @return type 商品类型：1非实物 2实物 3积分兑换
     * @return status 状态：1正常 2通过 3未通过 4下架
     */
    public function supplierGoodsListsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'参数错误');

            $store_id = Stores::where('user_id',$user_id)->where('status','<>',-1)->value('id');
            if (empty($store_id)) return $this->writeJson(1,null,'您还未开店，请先去开店');
            $fields = ['id','title','thumb','price','description','view','salenum','type','status','cate_id','cate_pid'];
            $lists = Goods::apiLists(['store_id'=>$store_id], $fields, $page, 10);

            foreach ($lists as $key=>$val){
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
                $lists[$key]['salenum'] = strlen($val['salenum'])>=4 ? '999+' : $val['salenum'];
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品基本信息
     * @param url home.user.supplierGoodsApi
     * @param Request $request
     */
    public function supplierGoodsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $good_id = (int)$request->get('good_id');
            if (empty($good_id)) return $this->writeJson(1,null,'参数错误');
            $lists = Goods::details(['id'=>$good_id]);
            $lists['cid'] = [$lists['cate_pid'],$lists['cate_id']];
            $lists['categorys'] = GoodsCategorys::getCascaderList();//获取分类
//            if (!empty($lists['images'])){
//                foreach ($lists['images'] as $key => $val){
//                    $images[$key]['name'] = substr($val,strripos($val,"/")+1);
//                    $images[$key]['url'] = $val;
//                }
//            }else{
//                $images = [];
//            }
//            $lists['images'] = $images;

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商添加、修改商品
     * @param url home.user.supplierGoodsUpdateApi
     * @param Request $request
     * @param thumb 封面图
     * @param title 标题
     * @param cate_pid 一级分类id
     * @param cate_id 二级分类id
     * @param description 简介
     * @param content 描述
     * @param images 多图（逗号隔开）
     * @param price 价格
     * @param link_name 联系人
     * @param link_phone 联系人电话
     * @param link_department 联系人部门
     * @param link_position 联系人职位
     * @param storage 库存
     * @param freight 运费
     * @param type 类型：1非实物 2实物
     */
    public function supplierGoodsUpdateApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
                if($userinfo['type'] != 2)  return $this->writeJson(1,null,'HR不能发布产品!');
            }

            $store = Stores::where(['user_id'=>$user_id])->where('status','<>',-1)->first(['id','store_name','status']);
            if (empty($store)){
                return $this->writeJson(1,null,'请先开店');
            } else{
                if($store['status'] == 2){
                    return $this->writeJson(1,null,'店铺正在审核中，请等待审核通过');
                }elseif ($store['status'] == 3){
                    return $this->writeJson(1,null,'店铺审核失败，请重新上传店铺资料');
                }elseif($store['status'] == 4){
                    return $this->writeJson(1,null,'店铺已关闭，请前往个人中心查看原因并联系管理员');
                }
            }

//            $good_id = (int)$request->post('good_id');

            $data = $request->all();
            $good_id = $data['id'];
            $data['store_id'] = $store['id'];
            $data['store_name'] = $store['store_name'];
            $data['status'] = 2;
//            $images_str = $request->post("images");//图片
//            if (!empty($images_str)){
//                $images = explode(',',$images_str);
//            }else{
//                $images = '';
//            }
//            $data = [
//                'store_id' => $store['id'],
//                'store_name' => $store['store_name'],
//                'thumb' =>  (string)$request->post('thumb'),
//                'title' => (string)$request->post('title'),
//                'cate_pid' => (int)$request->post('cate_pid'),
//                'cate_id' => (int)$request->post('cate_id'),
////                'description' => (string)$request->post('description'),
//                'content' => (string)$request->post('content'),
//                'images' => $images,
//                'price' => (string)$request->post('price'),
//                'link_name' => (string)$request->post('link_name'),
//                'link_phone' => (string)$request->post('link_phone'),
//                'link_department' => (string)$request->post('link_department'),
//                'link_position' => (string)$request->post('link_position'),
//                'storage' => (int)$request->post('storage'),
//                'freight' => (int)$request->post('freight'),
//                'type' => (int)$request->post('type'),
//                'status' => 2,
//            ];
            if (!empty($data['link_phone']) && !preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $data['link_phone'])){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            //生成未处理数据给后台
            $maps = [
                'title' => '未处理商品：'.$data['title'],
                'type' => 3,
            ];
            if(!empty($good_id)){
//                dd($data);
                $info = Goods::findOrFail($good_id);
                $info->update($data);
                AdminAgencys::create($maps);
                return $this->writeJson(0,null,'修改商品成功,请等待平台审核!');
            }else{
                $title = Goods::where('title',$data['title'])->where('status','<>',-1)->value('title');
                if (!empty($title)){
                    return $this->writeJson(1,null,'该商品名称已存在，请重新输入');
                }
                Goods::create($data);
                AdminAgencys::create($maps);
                return $this->writeJson(0,null,'新增商品成功,请等待平台审核!');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商添加、修改商品右侧-该商品发布供应商
     * @param url home.user.supplierGoodsStoreApi
     * @return bool
     */
    public function supplierGoodsStoreApi(){
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }
//            else{
//                if($userinfo['status'] == 2){
//                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
//                }elseif($userinfo['status'] == 3){
//                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
//                }elseif ($userinfo['status'] == 4){
//                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
//                }
//            }
            //查询当前登录供应商
            $fields = ['id','user_id','store_name','logo','cate_id','description','call','view','service','credit','hot','quality','credit'];
            $lists = Stores::details(['user_id'=>$user_id],$fields);
            $lists['link'] = ['master_name'=>'','master_phone'=>''];
            $lists['view'] = strlen($lists['view'])>=4 ? '999+' : $lists['view'];
            $lists['call'] = strlen($lists['call'])>=4 ? '999+' : $lists['call'];
            $lists['service'] = strlen($lists['service'])>=4 ? '999+' : $lists['service'];
            $lists['bool'] = false;
            //获取当前店铺所属用户的区域
            $area_id = Users::where('id',$lists['user_id'])->where('status','<>',-1)->value('area');
            $lists['area'] = Areas::where('id',$area_id)->where('status','<>',-1)->value('name');
            //擅长领域
            if (!empty($lists['cate_id'])){
                $cate_id = explode(',',$lists['cate_id']);
                foreach ($cate_id as $kk => $vv){
                    $pidTree[] = Categorys::where(['id'=>$vv])->first('name');
                }
            }else{
                $pidTree['name'] = [];
            }
            $lists['cate_name'] = $pidTree;
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品上下架
     * @param url home.user.supplierGoodsStateApi
     * @param goods_id 商品id
     * @param type 1上架 2下架
     * @return bool
     */
    public function supplierGoodsStateApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $goods_id = (int)$request->get('goods_id');
            $type = (int)$request->get('type');
            if(empty($goods_id) || empty($type)) return $this->writeJson(1,null,'参数错误');
            $info = Goods::findOrFail($goods_id);
            if ($info['status'] == 1 || $info['status'] == 4){
                if($type == 1){//上架
                    $info->update(['status' => 1]);
                }elseif($type == 2){//下架
                    $info->update(['status' => 4]);
                }
                return $this->writeJson(0,null,'操作成功');
            }elseif ($info['status'] == 2){
                return $this->writeJson(0,null,'该商品正在审核中');
            }elseif ($info['status'] == 3){
                return $this->writeJson(0,null,'该商品审核失败，请尽快修改商品信息');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值生成订单
     * @param url home.user.supplierRechargeOrderApi
     * @param money 充值金额
     */
    public function supplierRechargeOrderApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $money = (int)$request->post('money');//充值金额
            //余额日志记录
            $balance = [
                'user_id' => $user_id,
                'user_name' => $userinfo['username'],
                'money' => $money,
                'balance' => '+'.$money,
                'pay_sn' => Buys::makePaySn($user_id),
                'desc' => '余额充值',
                'type' => 3,
                'pay_state' => 1,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
            $balance_id = BalanceLogs::insertGetId($balance);
            return $this->writeJson(0,$balance_id,'生成成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值订单信息
     * @param url home.user.supplierRechargeDataApi
     * @param balance_id 充值订单id
     */
    public function supplierRechargeDataApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return $this->writeJson(1,null,'用户不存在,请联系客服!');
            }else{
                if($userinfo['status'] == 2){
                    return $this->writeJson(1, null,'账户正在审核中，请等待审核!');
                }elseif($userinfo['status'] == 3){
                    return $this->writeJson(1, null,'账号未通过审核，请前往个人中心查看原因并重新提交审核!');
                }elseif ($userinfo['status'] == 4){
                    return $this->writeJson(1, null,'账号已锁定，请前往个人中心查看原因或联系客服!');
                }
            }

            $balance_id = (int)$request->get('balance_id');//充值订单id
            $lists = BalanceLogs::where(['id'=>$balance_id,'user_id'=>$user_id])->first();

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值支付
     * @param url home.user.supplierRecharge
     * @param pay_order 充值订单号
     */
    public function supplierRecharge(Request $request){
        try{
            //根据订单信息，生成支付二维码
            $pay_order = $request->post('pay_order');//充值订单号
            $order_info = BalanceLogs::where(array('pay_sn'=>$pay_order))->first();
            $detail = '余额充值支付订单';
            $notify_url = route('home.payment.balanceNotifyUrl');
            $payment = new PaymentController();
            $ret=$payment->wxPayment($pay_order, $order_info['user_id'],$order_info['balance'],'支付订单',$detail,$notify_url);

            if($ret['code'] == 1){
                return $this->writeJson(0,$ret['data']);
            }else{
                return $this->writeJson(1,$ret);
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 对象转换数组
     * @param $e StdClass对象实例
     * @return array|void
     */
    public function objectToArray($e)
    {
        $e = (array)$e;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'resource') return;
            if (gettype($v) == 'object' || gettype($v) == 'array')
                $e[$k] = (array)$this->objectToArray($v);
        }
        return $e;
    }
}